import { Loading } from 'element-ui';
import router from '../router'
import { Message } from 'element-ui'
import { DRcookie } from '@/assets/js/common.js'
let instance;
window.spinner = {}

let skipList = [
    'geographyCity',
    'geographyArea',
    'geographyShop',
    'supplierShop/getList'
]

let operations = {
    show: config => {
        // 添加token到header
        let tH = DRcookie.get('tH'), tF = DRcookie.get('tF')
        // 排除掉登录的接口
        tH && tF && !config.url.includes('login') && (config.headers.Authorization = tH + " " + tF)

        let showSpinner = true
        skipList.map(ele => {
            config.url.includes(ele) && (showSpinner = false)
        })
        // 加载中的遮罩
        if(showSpinner) {
            instance = Loading.service({ 
                fullscreen: true,
                text: spinner.text || '加载中...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.2)'
            });
            setTimeout(() => {
                spinner.text = ''
                instance && instance.close()
            }, 1500)
        }
    },
    hide: (res)  => {
        if(res.data.code == '0000006' || res.data.code == '1000005') {
            Message({
                type: 'warning',
                message: res.data.msg
            })
            router.replace({name: 'login'})
        }
        setTimeout(() => {
            spinner.text = ''
            instance && instance.close()
        }, 500)
    }
}

export default operations